//
//  QDPageDetailController.m
//  simulateQuickDoctor
//
//  Created by 梁家伟 on 17/3/26.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDPageDetailController.h"
#import "QDNetworkTool.h"
#import "QDCarePatientModel.h"
#import "QDPickDocotrDateCell.h"

@interface QDPageDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)UILabel* label;
@property(nonatomic,strong)NSMutableArray* dataDict;
@property(nonatomic,strong)NSArray<QDDutyModel*>* modelArrData;
@property(nonatomic,strong)QDCarePatientModel* careModel;
@property(nonatomic,weak)UITableView* tableView;
@property(nonatomic,strong)NSDictionary* materialDict;
@property(nonatomic,strong)NSDictionary* introDict;
@property(nonatomic,strong)NSDictionary* dutyDict;
@end

@implementation QDPageDetailController{
    
}

-(instancetype)initWithIndex:(NSUInteger)index{
    
    if(self =  [super init]){
        self.index = index;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadPageData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)setupUI{
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    if(_index <= 1){
        UILabel* label = [UILabel cz_labelWithText:@"1" fontSize:15 color:[UIColor blackColor]];
        [self.view addSubview:label];
        label.textAlignment = NSTextAlignmentLeft;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        
        _label = label;
        
    }else{
        
        UIButton* helpOnlineBtn = [UIButton cz_textButton:@"在线客服" fontSize:18 normalColor:[UIColor whiteColor] selectedColor:[UIColor whiteColor]];
        
        helpOnlineBtn.layer.cornerRadius = 8;
        helpOnlineBtn.layer.masksToBounds = YES;
        
        [helpOnlineBtn setBackgroundColor:PPColor];
        [self.view addSubview:helpOnlineBtn];
        [helpOnlineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(30);
            make.right.equalTo(self.view).offset(-30);
            make.bottom.equalTo(self.view).offset(-12);
            make.height.offset(36);
        }];
        
        UITableView* tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        
        tableView.backgroundColor = [UIColor whiteColor];
        
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 36, 0);
        
        tableView.tableFooterView = [UIView new];
        tableView.separatorInset = UIEdgeInsetsZero;
        tableView.bounces = NO;
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.view);
            make.bottom.equalTo(helpOnlineBtn.mas_top).offset(-12);
        }];
        
        tableView.delegate = self;
        tableView.dataSource = self;
        
        _tableView = tableView;
        
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 200;
        
        [tableView registerClass:[QDPickDocotrDateCell class] forCellReuseIdentifier:@"date"];
    }
}


#pragma -mark 代理方法


#pragma -makr 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _modelArrData.count;
}


- (QDPickDocotrDateCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QDPickDocotrDateCell * cell = [tableView dequeueReusableCellWithIdentifier:@"date" forIndexPath:indexPath];
    
    QDDutyModel* model = _modelArrData[indexPath.row];
    model.zuozhen_fee = _careModel.zuozhen_fee;
    model.offer_time = _careModel.offer_time;
    
    cell.model = model;
    
    return cell;
}


/**
 网络加载医生简介数据
 */
- (void)loadPageData{
    
    NSString* urlString = @"";
    
    switch (_index) {
        case 0:
            urlString = @"http://iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
            
            if(_materialDict != nil){
                [self reloadTextData:_materialDict];
                return;
            }
            
            
            break;
        case 1:
            urlString = @"http://iosapi.itcast.cn/doctor/getIntroduction.json.php";
            
            if(_introDict != nil){
                [self reloadTextData:_materialDict];
                return;
            }
            
            break;
        case 2:
            urlString = @"http://iosapi.itcast.cn/doctor/doctorDuties.json.php";
            
            if(_dutyDict != nil){
                [self reloadDutyDate:_dutyDict];
                return;
            }
            
            break;
    }
    
    
    NSDictionary* postParams = @{@"doctor_id":@300000315};
    
    
    QDNetworkTool* tool = [QDNetworkTool sharedTool];
    
    tool.requestSerializer = [AFJSONRequestSerializer serializer];
    
//    [QDBannerTools pp_BannerTitle:@"正在加载.." andTime:0.3];
    
    [tool requestWithHTTPMethod:@"POST" URLString:urlString parameters:postParams CallBack:^(id responseObj) {
        
        if(responseObj != nil){
            
            NSString* result = responseObj[@"msg"];
            
            if([result isEqualToString:@"OK"]){
                
                NSDictionary* dataDict = responseObj[@"data"];
                
                switch (_index) {
                    case 0:
                    case 1:
                        [self reloadTextData:dataDict];
                        break;
                    case 2:
                        [self reloadDutyDate:dataDict];
                        break;
                }
                
            }else{
                NSLog(@"post params error");
            }
            
        }else{
            NSLog(@"load error");
        }
    }];
    
    
}

- (void)reloadDutyDate:(NSDictionary*)dict{
    
    _dutyDict = dict;
    
    NSArray* zuozhen_info_arr = dict[@"zuozhen_infos"];
    NSMutableDictionary* mDict = [NSMutableDictionary dictionaryWithDictionary:zuozhen_info_arr.firstObject];
    
    [mDict setObject:dict[@"duties"] forKey:@"duties"];
    
    QDCarePatientModel* careModel = [QDCarePatientModel new];
    
    [careModel setValuesForKeysWithDictionary:mDict];
    
    _careModel = careModel;
    
    _modelArrData = _careModel.duties;
    
    
    [_tableView reloadData];
}

- (void)reloadTextData:(NSDictionary*)dict{
    
    //接诊条件
    if(_index == 0){
    
        _materialDict = dict;
        
        NSString* receiving_setting_extra = dict[@"receiving_setting_extra"];
        
        NSArray<NSString*>* reference_material = dict[@"receiving_settings"];
        
        NSString* materialStr = [NSString string];
        
        for (NSString* material in reference_material) {
           materialStr = [materialStr stringByAppendingFormat:@"  %@\n\n",material];
        }
        
        NSString* resultStr = [NSString stringWithFormat:@"%@\n\n%@",receiving_setting_extra,materialStr];
        
        _label.text = resultStr;
        
    }else{//医生简介
        
        _introDict = dict;
        
        NSString* introStr = dict[@"introduction"];

        
        _label.text = introStr;
        
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
